package com.qianfeng.dbsm.controller;


import com.qianfeng.dbsm.common.ResponseData;
import com.qianfeng.dbsm.entity.TbOrderCommission;
import com.qianfeng.dbsm.service.*;
import com.qianfeng.dbsm.vo.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("orderManage")
@ApiOperation(value = "订单管理", notes = "管理订单")
public class OrderController {

    @Autowired
    TbOrderService orderService;

    @Autowired
    TbOrderLogisticsService logisticsService;

    @Autowired
    TbOrderInvoiceService orderInvoiceService;

    @Autowired
    TbOrderReturnsService orderReturnsService;

    @Autowired
    TbOrderCommissionService commissionService;

    @ApiOperation(value = "1-2创建订单-通过购物车提交", notes = "创建订单操作:购物车编号使用 逗号隔开：例如1,2,3,4,5")
    @PostMapping("createOrderByCart")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "cartIds",dataType = "String",value = "购物车编号，通过英文，隔开例如：1,2,3,4")
    })
    public ResponseData createOrderByCart(String cartIds){

        String orderNumber = orderService.generateOrderByCart(cartIds);
        ResponseData responseData = new ResponseData();
        responseData.setCode(20000);
        responseData.setData(orderNumber);
        responseData.setMessage("创建订单成功!");
        return responseData;
    }

    @ApiOperation(value = "1-1创建订单-通过页面直接购买", notes = "创建订单操作:用户直接从页面进行下单操作")
    @PostMapping("generateOrderByNoCart")
    public ResponseData generateOrderByNoCart(GenerateOrderVo generateOrderVo){

        String orderNumber = orderService.generateOrderByNoCart(generateOrderVo);
        ResponseData responseData = new ResponseData();
        responseData.setCode(20000);
        responseData.setData(orderNumber);
        responseData.setMessage("创建订单成功!");
        return responseData;
    }

    @ApiOperation(value = "8-1查询用户订单(个人用户接口)", notes = "通过会员编号获取订单列表-该接口用于个人用户登录当前账号查看本人订单")
    @GetMapping("queryOrderInfoByMemberId")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "memberId",value = "用户编号（不填则该字段不参与条件查询）",dataType = "String"),
            @ApiImplicitParam(name="orderState",value = "订单状态:来自codemap表（1-7）（不填则该字段不参与条件查询）",dataType = "String"),
            @ApiImplicitParam(name="page",value = "当前页码，不填默认查第1页",dataType = "String"),
            @ApiImplicitParam(name="pageSize",value = "每页多少条，不填默认10条",dataType = "String")
    })
    public ResponseData queryOrderInfoByMemberId(String memberId,String orderState,String page,String pageSize){
        ResponseData rd = new ResponseData();
        List<OrderVo> orderVos = orderService.queryOrderInfoByMemberId(memberId, orderState, page, pageSize);
        rd.setCode(20000);
        rd.setMessage("查询成功");
        rd.setData(orderVos);
        return rd;
    }

    @ApiOperation(value = "8-2查询用户订单(后台管理接口)", notes = "条件查询订单列表-该接口用于后台管理订单情况，条件为空自动过滤该条件")
    @GetMapping("queryOrderInfoByParam")
    public ResponseData queryOrderInfoByParam(OrderQueryParamVo queryParamVo){
        ResponseData rd = new ResponseData();
        List<OrderVo> orderVos = orderService.queryOrderInfoByParam(queryParamVo);
        rd.setCode(20000);
        rd.setMessage("查询成功");
        rd.setData(orderVos);
        return rd;
    }

    @ApiOperation(value = "9修改订单状态", notes = "通过订单编号修改订单为指定状态")
    @PutMapping("updateOrderInfo")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "orderNumber",value = "订单编号",dataType = "String"),
            @ApiImplicitParam(name="orderState",value = "订单状态:来自codemap表（1-7）",dataType = "String")
    })
    public ResponseData updateOrderInfo(String orderNumber,Integer orderState){
        ResponseData rd = new ResponseData();
        orderService.updateOrderInfo(orderNumber,orderState);
        rd.setCode(20000);
        rd.setMessage("更新成功");
        rd.setData(null);
        return rd;
    }
    @ApiOperation(value = "3.创建物流信息（卖家发货）", notes = "订单发货操作，生成物流相关信息")
    @PostMapping("createLogistics")
    public ResponseData createLogistics(LogisticsVo logisticsVo){
        ResponseData rd = new ResponseData();
        int line = logisticsService.createLogistics(logisticsVo);
        if(line != 0 ){
            rd.setCode(20000);
            rd.setMessage("创建物流信息成功");
            rd.setData(null);
        }else{
            rd.setCode(40000);
            rd.setMessage("创建物流信息失败");
            rd.setData(null);
        }
        return rd;
    }

    @ApiOperation(value = "5-1开具发票-个人发票", notes = "根据订单编号-开具个人发票")
    @PostMapping("createInvoiceByPersonal")
    public ResponseData createInvoiceByPersonal(PersonalInvoiceVo personalInvoiceVo){
        ResponseData rd = new ResponseData();
        int line = orderInvoiceService.createInvoiceByPersonal(personalInvoiceVo);
        if(line != 0 ){
            rd.setCode(20000);
            rd.setMessage("个人发票开具成功!!!");
            rd.setData(null);
        }else{
            rd.setCode(40000);
            rd.setMessage("个人发票开具失败!!!");
            rd.setData(null);
        }
        return rd;
    }
    @ApiOperation(value = "5-2开具发票-增值税发票", notes = "根据订单编号-开具增值税发票")
    @PostMapping("createInvoiceByCompany")
    public ResponseData createInvoiceByCompany(CompanyInvoiceVo companyInvoiceVo){
        ResponseData rd = new ResponseData();
        int line = orderInvoiceService.createInvoiceByCompany(companyInvoiceVo);
        if(line != 0 ){
            rd.setCode(20000);
            rd.setMessage("增值税发票开具成功!!!");
            rd.setData(null);
        }else{
            rd.setCode(40000);
            rd.setMessage("增值税发票开具失败!!!");
            rd.setData(null);
        }
        return rd;
    }
    @ApiOperation(value = "6.退货操作", notes = "用户根据订单编号进行退货处理")
    @PostMapping("createOrderReturns")
    public ResponseData createOrderReturns(OrderReturnsVo orderReturnsVo){
        ResponseData rd = new ResponseData();
        String returnNo = orderReturnsService.createOrderReturns(orderReturnsVo);
        if("".equals(returnNo) || null == returnNo){
            rd.setCode(40000);
            rd.setMessage("生成退货信息失败!!!");
            rd.setData(null);
        }else{
            rd.setCode(20000);
            rd.setMessage("生成退货信息成功!!!");
            rd.setData(returnNo);
        }
        return rd;
    }

    @ApiOperation(value = "7.查询分润情况", notes = "条件查询分润情况，相应字段为空默认不参与条件查询")
    @GetMapping("queryCommissionListByParam")
    public ResponseData queryCommissionListByParam(CommissionQueryParamVo commissionQueryParamVo){
        ResponseData rd = new ResponseData();
        List<TbOrderCommission> orderCommissionList = commissionService.queryCommissionListByParam(commissionQueryParamVo);
        if(orderCommissionList == null){
            rd.setCode(40000);
            rd.setMessage("查询该订单分润情况失败!!!");
            rd.setData(null);
        }else{
            rd.setCode(20000);
            rd.setMessage("查询该订单分润情况成功!!!");
            rd.setData(orderCommissionList);
        }
        return rd;
    }

    @ApiOperation(value = "4.确认收货", notes = "用户点击确认收货执行业务逻辑")
    @PostMapping("confirmOrderCompleted")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "订单编号",name = "orderNumber",dataType = "String")
    })
    public ResponseData confirmOrderCompleted(String orderNumber){
        ResponseData rd = new ResponseData();
        if(orderService.confirmOrderCompleted(orderNumber)){
            rd.setCode(20000);
            rd.setMessage("确认收货成功！！！！");
        }else {
            rd.setCode(40000);
            rd.setMessage("确认收货失败！！！！");
        }
        return rd;
    }

    @ApiOperation(value = "2.支付操作", notes = "这里是模拟用户支付操作，方便在swagger演示整套流程")
    @PostMapping("gotoPay")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "订单编号",name = "orderNumber",dataType = "String"),
            @ApiImplicitParam(value = "支付方式：1.支付宝 2.微信 3.银联 4.云闪付",name = "payType",dataType = "String")
    })
    public ResponseData gotoPay(String orderNumber,String payType){
        ResponseData rd = new ResponseData();
        if(orderService.gotoPay(orderNumber,payType)){
            rd.setCode(20000);
            rd.setMessage("支付成功！！！！");
        }else {
            rd.setCode(40000);
            rd.setMessage("支付失败！！！！");
        }
        return rd;
    }
    @ApiOperation(value = "10.分润订单审核", notes = "审核人员对分润订单进行审核")
    @PostMapping("auditCommission")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "订单编号",name = "orderNumber",dataType = "String"),
            @ApiImplicitParam(value = "审核结果：1:审核;2:不通过审核",name = "auditStatus",dataType = "Integer")
    })
    public ResponseData auditCommission(String orderNumber,Integer auditStatus){
        ResponseData rd = new ResponseData();
        if(commissionService.auditCommission(orderNumber,auditStatus)){
            rd.setCode(20000);
            rd.setMessage("订单审核成功！！！！");
        }else {
            rd.setCode(40000);
            rd.setMessage("订单审核失败！！！！");
        }
        return rd;
    }


}
